*===============================================================================
* Date: March 2024
* Authors: Gary W. Cox, Jon H. Fiva & Max-Emil M. King 
* Title: "Bound by Borders: Voter Mobilization through Social Networks"
* British Journal of Political Science
*===============================================================================

* README:

* This replication package produces all exhibits in the paper (excluding maps).

* Files generally need to be run in order. 

* The analyses were conducted using Stata/SE 17.0 for Windows.
* The following packages must be installed:
* estout, coefplot, blindschemes, spmap, carryforward, grc1leg

* Random seed is set on line 11 in the file "5l_Placebosim.do"

* It is not feasible to run the programs on a standard desktop machine.
* The code was last run on the Service for Sensitive Data platform (TSD), 
* with the following specifications:
* - Processor: Intel Xeon Platinum 8268 CPU @ 2.90 GHz (16 processors)
* - Memory: 128 Gb
* - Windows edition: Windows Server 2019 Standard, version 1809
* On this system, it takes approximately 12 hours to run through all code.^

* The data utilized in this study are sourced from Norwegian administrative
* registers and provided under loan from Statistics Norway, making them legally
* restricted from further sharing. Researchers interested in accessing these 
* data can apply to Statistics Norway using:
* https://www.ssb.no/en/data-til-forskning/utlan-av-data-til-forskere/
* soknad-offentlig-myndighet.

* ^Not including the program 5l_Placebosim.do, which was run on a
* separate system due to excess memory requirements.

*===============================================================================



clear all

* Set paths and directory
* =======================

global root "..."
global figs "figures" 						// Output figures
global tbls "tables" 						// Output tables
global dta_raw "..." 						// Register data
global dta_ext "data\external"				// External data (e.g. survey files)
global dta "data\dta"						// Output data
global do "do"								// do-files
global prog "do/programs"					// Supplementary programs

cd "$root"


* 1: Data import
* ==============
run $do/1a_harmonize_knrbsu.do 		// Creates harmonize_knr.dta and harmonize_bsu.dta
run $do/1b_ImportPers.do 			// Creates ImportPers.dta
run $do/1c_ImportRelatives.do 		// Creates ImportParents.dta, ImportSiblings.dta, and ImportChildren.dta
run $do/1d_ImportGeo.do 			// Creates ImportGeo.dta, ImportGeo_raw.dta
run $do/1e_ImportEduc.do 			// Creates ImportEduc.dta
run $do/1f_ImportOccup.do 			// Creates ImportOccup15.dta, ImportOccup19.dta
run $do/1g_ImportWork15.do 			// Creates ImportWork15.dta
run $do/1h_ImportIncome.do 			// Creates ImportIncome.dta
run $do/1i_ImportTurnout.do 		// Creates ImportTurnout.dta
run $do/1j_ImportDistance.do 		// Creates ImportDistance.dta
run $do/1k_ImportPoliticians.do 	// Creates ImportPoliticians.dta
run $do/1l_ImportMarriage.do 		// Creates ImportMarriage.dta
run $do/1m_ImportDonations.do 		// Creates ImportDonations.dta


* 2: Construct networks
* =====================
run $do/2a_NetworkFam.do 			// Creates NetworkFam1.dta and NetworkFam2.dta
run $do/2b_NetworkWork.do 			// Creates NetworkWork1.dta and NetworkWork2.dta
run $do/2c_NetworkImm.do 			// Creates NetworkImm1.dta, NetworkImm2.dta, NetworkImm3a.dta, and NetworkImm3b.dta
run $do/2d_Network2step.do 			// Creates NetworkFam1Work1.dta, NetworkWork1Fam1.dta, NetworkFam1Imm1.dta, and NetworkFam1Imm1.dta


* 3: Prepare data sets
* ====================
run $do/3a_Population.do 			// Creates Population.dta
run $do/3b_Turnout_knr.do 			// Creates Turnout_knr.dta
run $do/3c_PanelVoters.do 			// Creates PanelVoters.dta
run $do/3d_PanelPoliticians.do 		// Creates PanelPoliticians.dta
run $do/3e_EstimationSample.do 		// Creates EstimationSample.dta
run $do/3f_SurveyAttitudes.do 		// Creates SurveyAttitudes.dta
run $do/3g_prWin.do 				// Creates prWin.dta


* Main exhibits
* =============
run $do/4a_SurveyVoting.do 			// Creates SurveyVoting.pdf (Figure 1)
run $do/4b_Baseline.do 				// Creates Baseline.tex (Table 1)
run $do/4c_DistplotsNarrow.do 		// Creates DisplotsNarrow.pdf (Figure 3)
run $do/4d_Twostep.do 				// Creates Twostep.tex (Table 2)
run $do/4e_NativesImmigrants.do 	// Creates NativesImmigrants.tex (Table 3)
run $do/4f_MaxeffCandidacies.do 	// Creates MaxeffCandidacies.tex (Table 4) 


* Online appendix A
* =================
run $do/5a_SummaryMunicipalities.do // Creates SummarySample.tex (Table A.1)
run $do/5b_SummaryNetworks.do 		// Creates SummaryNetworks.tex (Table A.2)
run $do/5c_SummaryPoliticians.do 	// Creates SummaryPoliticians.tex (Table A.3)
run $do/5d_SplitFamily.do 			// Creates SplitFamily.tex (Table A.4)
run $do/5e_SplitCoworkers.do 		// Creates SplitCoworkers.tex (Table A.5)
run $do/5f_SplitImmigrants.do 		// Creates SplitImmigrants.tex (Table A.6)
run $do/5g_Baseline_geotime.do 		// Creates Baseline_geotime.tex (Table A.7)
run $do/5h_SummaryEfficiency.do 	// Creates SummaryEfficiency.tex (Table A.8)
run $do/5i_ImmigrantsAlternate.do 	// Creates ImmigrantsAlternate.pdf (Table A.9)

run $do/5j_DistributionsSize.do 	// Creates DistributionsSize.pdf (Figure A.3)
run $do/5k_DistributionsDistance.do // Creates DistributionsDistance.pdf (Figure A.4)
*run $do/5l_Placebosim.do 			// Creates Placebosim.pdf (Figure A.5)
run $do/5m_DistplotsBroad.do 		// Creates DisplotsBroad.pdf (Figure A.6)
run $do/5n_DistplotsBalance.do 		// Creates DisplotsBalance.pdf (Figures A.7 - A.10)
run $do/5o_ImmigrantsFamilyParty.do // Creates NativesImmigrantsParty.pdf (Figure A.11)
run $do/5p_SurveyImmigrants.do 		// Creates SurveyImmigrants.pdf (Fig A.12)
run $do/5q_MaxeffSensitivity.do 	// Creates MaxeffSensitivity.pdf (Figure A.13) 


* Online appendix D
* =================
run $do/6a_prWinIllustration.do 	// Creates prWinIllustration.pdf (Fig D.1)
run $do/6b_SplitPrWin.do 			// Creates SplitPrWin.pdf (Fig D.2)


** END